import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import {LoginUsers, Users} from './data/user';
let _Users = Users;

import {mockProducts} from './data/products';

import {mockComments} from './data/comments';
import {mockSpecs} from './data/specs';

import {mockSysUsers} from './data/sysUser';
import {mockOrders} from './data/orders';
import {mockCategorys} from './data/Categorys';
import {mockCitys} from './data/citys';
import {mockStations} from './data/stations';

export default {
  /**
   * mock bootstrap
   */
  bootstrap() {
    let mock = new MockAdapter(axios);

    if (-1 === window.location.href.indexOf("http://localhost")) {
      return;
    }

    // mock success request
    mock
      .onGet('/success')
      .reply(200, {msg: 'success'});

    // mock error request
    mock
      .onGet('/error')
      .reply(500, {msg: 'failure'});

    //登录
    mock
      .onPost('/login')
      .reply(config => {
        let {username, password} = JSON.parse(config.data);
        return new Promise((resolve, reject) => {
          let user = null;
          setTimeout(() => {
            let hasUser = LoginUsers.some(u => {
              if (u.username === username && u.password === password) {
                user = JSON.parse(JSON.stringify(u));
                user.password = undefined;
                return true;
              }
            });

            if (hasUser) {
              resolve([
                200, {
                  code: 200,
                  msg: '请求成功',
                  user
                }
              ]);
            } else {
              resolve([
                200, {
                  code: 500,
                  msg: '账号或密码错误'
                }
              ]);
            }
          }, 1000);
        });
      });

    //获取用户列表
    mock
      .onGet('/userLikePhone')
      .reply(config => {
        let {name} = config.params;
        let mockUsers = _Users.filter(user => {
          // if (name && user.name.indexOf(name) == -1) return false;
          return true;
        });
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: "",
                data: {
                  users: mockUsers,
                  total: 200
                }
              }
            ]);
          }, 1000);
        });
      });

    //获取用户列表
    mock
      .onGet('/user/list')
      .reply(config => {
        let {name} = config.params;
        let mockUsers = _Users.filter(user => {
          if (name && user.name.indexOf(name) == -1) 
            return false;
          return true;
        });
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                users: mockUsers
              }
            ]);
          }, 1000);
        });
      });

    //获取用户列表（分页）
    mock
      .onGet('/user/listpage')
      .reply(config => {
        let {page, name} = config.params;
        let mockUsers = _Users.filter(user => {
          if (name && user.name.indexOf(name) == -1) 
            return false;
          return true;
        });
        let total = mockUsers.length;
        mockUsers = mockUsers.filter((u, index) => index < 20 * page && index >= 20 * (page - 1));
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                total: total,
                users: mockUsers
              }
            ]);
          }, 1000);
        });
      });

    //删除用户
    mock
      .onGet('/user/remove')
      .reply(config => {
        let {id} = config.params;
        _Users = _Users.filter(u => u.id !== id);
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: '删除成功'
              }
            ]);
          }, 500);
        });
      });

    //批量删除用户
    mock
      .onGet('/user/batchremove')
      .reply(config => {
        let {ids} = config.params;
        ids = ids.split(',');
        _Users = _Users.filter(u => !ids.includes(u.id));
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: '删除成功'
              }
            ]);
          }, 500);
        });
      });

    //编辑用户
    mock
      .onGet('/user/edit')
      .reply(config => {
        let {
          id,
          name,
          addr,
          age,
          birth,
          sex
        } = config.params;
        _Users.some(u => {
          if (u.id === id) {
            u.name = name;
            u.addr = addr;
            u.age = age;
            u.birth = birth;
            u.sex = sex;
            return true;
          }
        });
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: '编辑成功'
              }
            ]);
          }, 500);
        });
      });

    //新增用户
    mock
      .onGet('/user/add')
      .reply(config => {
        let {name, addr, age, birth, sex} = config.params;
        _Users.push({name: name, addr: addr, age: age, birth: birth, sex: sex});
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: '新增成功'
              }
            ]);
          }, 500);
        });
      });

    //获取商品列表
    mock
      .onGet('/product')
      .reply(config => {
        return new Promise((resolve, reject) => {
          console.log("获取商品列表:");
          console.log(mockProducts);
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: "",
                data: {
                  products: mockProducts,
                  total: 200
                }
              }
            ]);
          }, 1000);
        });
      });

    //添加商品
    mock
      .onDelete('/product/add')
      .reply(config => {
        console.log(config);
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: '添加成功'
              }
            ]);
          }, 500);
        });
      });
    //编辑商品
    mock
      .onDelete('/product/edit')
      .reply(config => {
        console.log(config);
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: '编辑成功'
              }
            ]);
          }, 500);
        });
      });

    //删除商品
    mock
      .onDelete('/product/remove')
      .reply(config => {
        console.log(config);
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: '删除成功'
              }
            ]);
          }, 500);
        });
      });

    //批量删除商品
    mock
      .onDelete('/product/batchremove')
      .reply(config => {
        console.log(config);
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: '删除成功'
              }
            ]);
          }, 500);
        });
      });
    //评论管理 获取评论列表
    mock
      .onGet('/comment')
      .reply(config => {
        return new Promise((resolve, reject) => {
          console.log("获取comment列表:");
          console.log(mockComments);
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: "",
                data: {
                  comments: mockComments,
                  total: 200
                }
              }
            ]);
          }, 1000);
        });
      });
    //删除评论
    mock
      .onDelete('/comment/remove')
      .reply(config => {
        console.log(config);
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: '删除成功'
              }
            ]);
          }, 500);
        });
      });

    //批量删除评论
    mock
      .onDelete('/comment/batchremove')
      .reply(config => {
        console.log(config);
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: '删除成功'
              }
            ]);
          }, 500);
        });
      });

    //商品规格 获取 商品规格列表
    mock
      .onGet('/specs')
      .reply(config => {
        return new Promise((resolve, reject) => {
          console.log("获取mockSpecs列表:");
          console.log(mockSpecs);
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: "",
                data: {
                  specss: mockSpecs,
                  total: 200
                }
              }
            ]);
          }, 1000);
        });
      });
    //删除商品规格
    mock
      .onDelete('/specs/remove')
      .reply(config => {
        console.log(config);
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: '删除成功'
              }
            ]);
          }, 500);
        });
      });

    //批量删除商品规格
    mock
      .onDelete('/specs/batchremove')
      .reply(config => {
        console.log(config);
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: '删除成功'
              }
            ]);
          }, 500);
        });
      });

    //系统管理列表
    mock
      .onGet('/adminUser')
      .reply(config => {
        return new Promise((resolve, reject) => {
          console.log("获取商品列表:");
          console.log(mockSysUsers);
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: "",
                data: {
                  users: mockSysUsers,
                  total: 200
                }
              }
            ]);
          }, 1000);
        });
      });
    //删除商品
    mock
      .onDelete('/adminUser/1')
      .reply(config => {
        console.log(config);
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: '删除成功'
              }
            ]);
          }, 500);
        });
      });

    //添加
    mock
      .onPost('/adminUser')
      .reply(config => {
        console.log(config);
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: '添加成功'
              }
            ]);
          }, 500);
        });
      });

    //编辑成功
    mock
      .onPut('/adminUser')
      .reply(config => {
        console.log(config);
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: '编辑成功'
              }
            ]);
          }, 500);
        });
      });

    //获取 详情
    mock
      .onGet('/adminUser/1')
      .reply(config => {
        console.log(config);
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: '成功',
                data: {
                  name: "4565",
                  pwd: "456456",
                  phone: "10010",
                  email: "10010@qq.com",
                  openId: "sdfdsfxvxvvxcxvxv",
                  role: "2"
                }
              }
            ]);
          }, 500);
        });
      });

    //订单管理
    mock
      .onGet('/orders')
      .reply(config => {
        return new Promise((resolve, reject) => {
          console.log("订单管理:");
          console.log(mockProducts);
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: "",
                data: {
                  products: mockOrders,
                  total: 200
                }
              }
            ]);
          }, 1000);
        });
      });

    //分类列表
    mock
      .onGet('/categorys')
      .reply(config => {
        return new Promise((resolve, reject) => {
          console.log("分类列表:");
          console.log(mockCategorys);
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: "",
                data: {
                  categorys: mockCategorys,
                  total: 200
                }
              }
            ]);
          }, 1000);
        });
      });
    //删除分类
    mock
      .onDelete('/categorys/remove')
      .reply(config => {
        console.log(config);
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: '删除成功'
              }
            ]);
          }, 500);
        });
      });

    //批量删除
    mock
      .onDelete('/categorys/batchremove')
      .reply(config => {
        console.log(config);
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: '删除成功'
              }
            ]);
          }, 500);
        });
      });

    //添加
    mock
      .onPost('/categorys')
      .reply(config => {
        console.log(config);
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: '添加成功'
              }
            ]);
          }, 500);
        });
      });

    //编辑成功
    mock
      .onPut('/categorys')
      .reply(config => {
        console.log(config);
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: '编辑成功'
              }
            ]);
          }, 500);
        });
      });

    mock
      .onGet('/citys')
      .reply(config => {
        return new Promise((resolve, reject) => {
          console.log("城市列表:");
          console.log(mockCitys);
          setTimeout(() => {
            resolve([
              200, {
                code: 200,
                msg: "",
                data: {
                  citys: mockCitys,
                  total: 200
                }
              }
            ]);
          }, 1000);
        });
      });

  /**
   * 高铁管理
   */
    //列表
    mock
    .onGet('/station')
    .reply(config => {
      return new Promise((resolve, reject) => {
        console.log("高铁列表:");
        console.log(mockStations);
        setTimeout(() => {
          resolve([
            200, {
              code: 200,
              msg: "",
              data: {
                stations: mockStations,
                total: 200
              }
            }
          ]);
        }, 1000);
      });
    });
  //删除
  mock
    .onDelete('/station/remove')
    .reply(config => {
      console.log(config);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([
            200, {
              code: 200,
              msg: '删除成功'
            }
          ]);
        }, 500);
      });
    });

  //批量删除
  mock
    .onDelete('/station/batchremove')
    .reply(config => {
      console.log(config);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([
            200, {
              code: 200,
              msg: '删除成功'
            }
          ]);
        }, 500);
      });
    });

  //添加
  mock
    .onPost('/station')
    .reply(config => {
      console.log(config);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([
            200, {
              code: 200,
              msg: '添加成功'
            }
          ]);
        }, 500);
      });
    });

  //编辑
  mock
    .onPut('/station')
    .reply(config => {
      console.log(config);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([
            200, {
              code: 200,
              msg: '编辑成功'
            }
          ]);
        }, 500);
      });
    });

  }
};